package com.tony.mindweather.MyTimer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tony.mindweather.entity.*;
import com.tony.mindweather.entity.Forecast;
import com.tony.mindweather.entity.SayLoveBean;
import com.tony.mindweather.entity.Weather;
import com.tony.mindweather.entity.WeatherResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Classname EmailReceiver
 * @Description TODO
 * @Date 2021/7/30 20:26
 * @Created by
 */
@Component
public class MyTimerToQQMail {
    /**
     * 邮箱号
     */
    @Value("${spring.mail.username}")
    private String email;

    @Value("sayloveurlAPIKEY")
    private String sayloveurl;

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Autowired
    private RestTemplate restTemplate;

    /*
    * 异步处理还是非常常用的，比如我们在网站上发送邮件，后台会去发送邮件，此时前台会造成响应不动，直到邮件发送完毕，响应才会成功，
    * 所以我们一般会采用多线程的方式去处理这些任务。
    *
    * 解决方案：
    *   给方法添加@Async注解
    *   SpringBoot就会自己开一个线程池，进行调用！但是要让这个注解生效，我们还需要在主程序上添加一个注解@EnableAsync ，开启异步注解功能；
    * */

//    @Async
//    @Scheduled(cron = "0 0 7/23 * * ? ")
    public void process() {
        SimpleMailMessage message = new SimpleMailMessage();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日E");
        String datestr=sdf.format(new Date());
        message.setSubject(datestr+"上海的天气");//主题

        String apiURL = "http://wthrcdn.etouch.cn/weather_mini?city=" + "上海";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiURL, String.class);// 发起天气请求
        ObjectMapper objectMapper = new ObjectMapper();
        WeatherResponse resp = null;
        String strBody = null;
        if (responseEntity.getStatusCodeValue() == 200) {
            strBody = responseEntity.getBody();
        }
        try {
            resp = objectMapper.readValue(strBody, WeatherResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        final Weather data = resp.getData();
        final String city = data.getCity();
        final String ganmao = data.getGanmao();
        final String wendu = data.getWendu();
        final Forecast forecast = data.getForecast().get(0);
        final String high = forecast.getHigh();
        final String low = forecast.getLow();
        final String type = forecast.getType();

        String showWeather = datestr+","+city+"的天气为"+type+" ,最高温度是:"+ high +" ,最低温度是:"+low+",当前温度为:"+wendu+",注意:"+ganmao+" ";

        // 处理土味情话
        String sayLoveAPIURL= "http://api.tianapi.com/saylove/index?key="+sayloveurl;
        final ResponseEntity<String> sayLoveString = restTemplate.getForEntity(sayLoveAPIURL, String.class);// 发起saylove 请求
        String sayloveBody = null;
        if (sayLoveString.getStatusCodeValue() == 200){
            sayloveBody = sayLoveString.getBody();
        }
        SayLoveBean sayLoveBean = null;
        try {
            sayLoveBean = objectMapper.readValue(sayloveBody, SayLoveBean.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        final String sayLoveContent = sayLoveBean.getNewslist().get(0).getContent();

        String showWeatherWithSayLove = showWeather+sayLoveContent+" ,早安呀,小宝贝,小猪爱你每一天,今天是小猪爱你的第"+2+"天,还会继续爱着小宝贝的呢。";

        message.setText(showWeatherWithSayLove); // 内容

        message.setTo("1360844239@qq.com"); // 接收者
        message.setFrom("1360844239@qq.com"); // 发送者
        mailSender.send(message);

    }

}
